package com.ruoyi.project.cardbox.inbound.controller;

import java.util.Arrays;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.cardbox.inbound.domain.OrderInbound;
import com.ruoyi.project.cardbox.inbound.service.IOrderInboundService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

/**
 * 入库单Controller
 * 
 * @author ruoyi
 * @date 2025-07-04
 */
@Controller
@RequestMapping("/cardbox/inbound")
public class OrderInboundController extends BaseController
{
    private String prefix = "cardbox/inbound";

    @Autowired
    private IOrderInboundService orderInboundService;

    @RequiresPermissions("cardbox:inbound:view")
    @GetMapping()
    public String inbound()
    {
        return prefix + "/inbound";
    }

    /**
     * 查询入库单列表
     */
    @RequiresPermissions("cardbox:inbound:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(OrderInbound orderInbound)
    {
        startPage();
        List<OrderInbound> list = orderInboundService.selectOrderInboundList(orderInbound);
        return getDataTable(list);
    }

    /**
     * 导出入库单列表(全部)
     */
    @RequiresPermissions("cardbox:inbound:export")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(OrderInbound orderInbound)
    {
        List<OrderInbound> list = orderInboundService.selectOrderInboundList(orderInbound);
        ExcelUtil<OrderInbound> util = new ExcelUtil<OrderInbound>(OrderInbound.class);
        List<String> footer = Arrays.asList(
                " 说明: ",
                " 按规定的附件配置生产；",
                " 纸板平整，不弯曲或折叠；",
                " 压紧到位，不隐藏包。"
        );

        return util.exportExcelWithFooter(list, "入库单数据", "入库单数据", footer);
//        return util.exportExcel(list, "入库单数据");
    }

    /**
     * 导出选中
     * @param ids
     * @return
     */
    @RequiresPermissions("cardbox:inbound:export")
    @Log(title = "入库单", businessType = BusinessType.EXPORT)
    @PostMapping("/exportSelected")
    @ResponseBody
    public AjaxResult exportSelected(@RequestParam("ids") List<String> ids) {
        List<OrderInbound> list = orderInboundService.selectOrderInboundByIds(ids);
        ExcelUtil<OrderInbound> util = new ExcelUtil<>(OrderInbound.class);
        List<String> footer = Arrays.asList(
                " 说明: ",
                " 按规定的附件配置生产；",
                " 纸板平整，不弯曲或折叠；",
                " 压紧到位，不隐藏包。"
        );

        return util.exportExcelWithFooter(list, "入库单数据", "入库单数据", footer);
//        return util.exportExcel(list, "入库单数据");
    }


//    /**
//     * 新增入库单
//     */
//    @RequiresPermissions("cardbox:inbound:add")
//    @GetMapping("/add")
//    public String add()
//    {
//        return prefix + "/add";
//    }
//
//    /**
//     * 新增保存入库单
//     */
//    @RequiresPermissions("cardbox:inbound:add")
//    @Log(title = "入库单", businessType = BusinessType.INSERT)
//    @PostMapping("/add")
//    @ResponseBody
//    public AjaxResult addSave(OrderInbound orderInbound)
//    {
//        return toAjax(orderInboundService.insertOrderInbound(orderInbound));
//    }

    /**
     * 修改入库单
     */
    @RequiresPermissions("cardbox:inbound:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        OrderInbound orderInbound = orderInboundService.selectOrderInboundById(id);
        mmap.put("orderInbound", orderInbound);
        return prefix + "/edit";
    }

    /**
     * 修改保存入库单
     */
    @RequiresPermissions("cardbox:inbound:edit")
    @Log(title = "入库单", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(OrderInbound orderInbound)
    {
        return toAjax(orderInboundService.updateOrderInbound(orderInbound));
    }

    /**
     * 删除入库单
     */
    @RequiresPermissions("cardbox:inbound:remove")
    @Log(title = "入库单", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(orderInboundService.deleteOrderInboundByIds(ids));
    }
}
